<?php
use think\Db;
//变更推荐人数
//wxy
//  $uid  被推荐人  $pid  推荐人  $num  +-  变更数量 可为负数 既减去
function changeRecom($uid,$num=1)
{
    //本人信息
    $userData=getUserInfoByUid($uid);
    //推荐人路径
    $pPath=array_values(array_filter(explode(",",$userData["path"])));
    $error=0;
    rsort($pPath);
    if(!empty($pPath))
    {
        foreach($pPath as $k=>$v)
        {
            //如果是直推的   则直推和团队都更改
            if($k==0)
            {
                //如果是加
                if($num>0)
                {
                    $res1= Db::name("users")->where("user_id",$v)->setInc("valid_recom",1);
                    $res2= Db::name("users")->where("user_id",$v)->setInc("valid_team",1);
                }else
                {
                    $res1= Db::name("users")->where("user_id",$v)->setDec("valid_recom",1);
                    $res2= Db::name("users")->where("user_id",$v)->setDec("valid_team",1);
                }
                if(!$res1||!$res2)
                {
                    $error++;
                }
            }else  //如果不是直推只更改团队
            {
                //如果是加
                if($num>0)
                {
                    $res1= Db::name("users")->where("user_id",$v)->setInc("valid_team",1);
                }else
                {
                    $res1= Db::name("users")->where("user_id",$v)->setDec("valid_team",1);
                }
                if(!$res1)
                {
                    $error++;
                }
            }
        }
    }
    if($error>0)
    {
        return false;
    }else
    {
        return true;
    }

}
//更改团队投资额
function changeTeamMoney($uid,$money)
{
    if($money==0)
    {
        return false;
    }
    //本人信息
    $userData=getUserInfoByUid($uid);
    $error=0;

    //推荐人路径
    $pPath=array_values(array_filter(explode(",",$userData["path"])));

    rsort($pPath);
    if(!empty($pPath))
    {
        foreach($pPath as $k=>$v)
        {
            if($money>0)
            {
                $res1= Db::name("users")->where("user_id",$v)->setInc("team_money",$money);
            }else
            {
                $res1= Db::name("users")->where("user_id",$v)->setDec("team_money",abs($money));
            }
            if(!$res1)
            {
                $error++;
            }
        }
    }
    if($error>0)
    {
        return false;
    }else
    {
        return true;
    }
}

//推荐奖励
function recommondAward($uid,$money)
{
    if($money==0)
    {
        return false;
    }
    //本人信息
    $userData=getUserInfoByUid($uid);
    //推荐配置
    $info = Db::name('setting')->select();
    foreach ($info as $k => $v) {
        $data[$v['setting_name']] = $info[$k]['setting_value'];
    }
    //推荐奖励金额

    //推荐人路径
    $pPath=array_values(array_filter(explode(",",$userData["path"])));
    $error=0;
    rsort($pPath);
    if(!empty($pPath))
    {
        foreach($pPath as $k=>$v)
        {
            $price=0;
            if($k>2)
            {
                break;
            }else
            {
                $price=round(($data["recommend_".($k+1)]/100)*$money,3);
                $res=changeBalance($v,1,0,$price,2,$data["recommend_".($k+1)],$uid);
                //改变佣金 system
                $res1=change_system($v,$price,3);
                if(!$res||!$res1)
                {
                    $error++;
                }
            }
        }
    }
    if($error>0)
    {
        return false;
    }else
    {
        return true;
    }
}
//变更金额
//用户id   变更金额类型  加减  变更金额 变更类型 百分比 对应user_id
function changeBalance($uid,$balance_type,$sign,$money,$from_type,$percent=0,$to_user_id="",$principal="")
{
    $blanceInfo=Db::name("balance")->where("user_id",$uid)->find();
    if(empty($blanceInfo))
    {
        $addBblance=array(
            "user_id"=>$uid,
        );
        $res=Db::name("balance")->insert($addBblance);
        if($res)
        {
            $blanceInfo=Db::name("balance")->where("user_id",$uid)->find();
        }
    }
    $balanceType="";
    if($balance_type==0)
    {
        $balanceType="balance";
    }else if($balance_type==1)
    {
        $balanceType="earnings";
    }else if($balance_type==2)
    {
        $balanceType="jetton";
    }
    if($sign==0)
    {
        $resUpdateBalance=Db::name("balance")->where("user_id",$uid)->setInc($balanceType,$money);
    }else if($sign==1)
    {
        if($blanceInfo[$balanceType]-$money<0)
        {
            return false;
        }
        $resUpdateBalance=Db::name("balance")->where("user_id",$uid)->setDec($balanceType,$money);
    }
    $arrInsertLog=array(
        "user_id"=>$uid,
        "to_user_id"=>$to_user_id,
        "sign"=>$sign,
        "percent"=>$percent,
        "money"=>$money,
        "balance_type"=>$balance_type,
        "from_type"=>$from_type,
        "principal"=>$principal,
        "create_time"=>time()
    );
    $resInsert=Db::name("balance_log")->insert($arrInsertLog);
    if($resUpdateBalance&&$resInsert)
    {
        return true;
    }else
    {
        return false;
    }
}
//获取用户信息
function getUserInfoByUid($uid)
{
    return  Db::name("users")->where("user_id",$uid)->find();
}
//数据返回
function AjaxReturn($code,$msg,$data=array(),$debug="")
{
    $returnData=array(
        "code"=>$code,
        "message"=>$msg,
        "debug"=>$debug
    );
    if(!empty($data))
    {
        $returnData["data"]=$data;
    }
    return json($returnData);
}





//业绩上级奖励
function performance($uid,$money)
{
    //本人信息
    $userData=getUserInfoByUid($uid);

    if(empty($userData))
    {
        return false;
    }
    //推荐人路径
    $pPath=array_values(array_filter(explode(",",$userData["path"])));
    $error=0;
    rsort($pPath);
    $award=array();
    $pUserData=array();
    if(!empty($pPath))
    {
        foreach($pPath as $k=>$v) {
            $price=0;//应加业绩金额
            $pUserData=array();//上级个人信息
            $award=array();//上级购买代理等级
            $pUserData = getUserInfoByUid($v);
            if (empty($pUserData)) {
                return false;
            }
            if ($pUserData["level"] == 1)
            {
                continue;
            }
            //获取投资
            $award=Db::name("invest")->where("id",$pUserData["level"])->find();
            if(empty($award))
            {
                return false;
            }
            if($k<20)
            {
                $field=getPercent(($k+1));
                $percent=$award[$field];
                if($percent==0||$percent=="")
                {
                    continue;
                }
                $price=round($money*($percent/100),3);
                //改变业绩
                $res=changeBalance($v,1,0,$price,1,$percent,$uid,$money);
                //改变业绩 system
                $res1=change_system($v,$price,2);
                if(!$res&&!$res1)
                {
                    $error++;
                }
            }else
            {
                break;
            }
        }
    }
    if($error>0)
    {
        return false;
    }else
    {
        return true;
    }
}
//获取当前推荐等级对应的字段
function getPercent($code)
{
    switch($code)
    {
        case 1 : return "first"; break;
        case 2 : return "second"; break;
        case 3 : return "third"; break;
        case 4 : return "fourth"; break;
        case 5 : return "fifth"; break;
        case 6 : return "sixth"; break;
        case 7 : return "seventh"; break;
        case 8 : return "eighth"; break;
        case 9 : return "ninth"; break;
        case 10 : return "tenth"; break;
        default : return "twentieth";
    }
}

//更改年月日金额
function change_system($uid,$money,$type)
{

    $systemData=Db::name("balance_system")->where("user_id",$uid)->find();
    if(empty($systemData))
    {
        $addSystem=array(
            "user_id"=>$uid,
            "year_time_earnings"=>date("Y"),
            "month_time_earnings"=>date("m"),
            "day_time_earnings"=>strtotime(date("Y-m-d")),
            "year_time_performance"=>date("Y"),
            "month_time_performance"=>date("m"),
            "day_time_performance"=>strtotime(date("Y-m-d")),
            "year_time_brokerage"=>date("Y"),
            "month_time_brokerage"=>date("m"),
            "day_time_brokerage"=>strtotime(date("Y-m-d")),
        );
        $res=Db::name("balance_system")->insert($addSystem);
        if($res)
        {
            $systemData=Db::name("balance_system")->where("user_id",$uid)->find();
        }
    }
    if($type==1)
    {
        $str="earnings";
    }
    if($type==2)
    {
        $str="performance";
    }
    if($type==3)
    {
        $str="brokerage";
    }
    if(time()-$systemData["day_time_".$str]<=86400&&$systemData["month_time_".$str]==date("m")&&$systemData["year_time_".$str]==date("Y"))
    {
        $updateData=array(

            "all_".$str=>$systemData["all_".$str]+$money,
            "year_".$str=>$systemData["year_".$str]+$money,
            "month_".$str=>$systemData["month_".$str]+$money,
            "day_".$str=>$systemData["day_".$str]+$money,
            "year_time_".$str=>date("Y"),
            "month_time_".$str=>date("m"),
            "day_time_".$str=>strtotime(date("Y-m-d")),
        );
    }else if(time()-$systemData["day_time_".$str]>86400&&$systemData["month_time_".$str]==date("m")&&$systemData["year_time_".$str]==date("Y"))
    {
        $updateData=array(
            "all_".$str=>$systemData["all_".$str]+$money,
            "year_".$str=>$systemData["year_".$str]+$money,
            "month_".$str=>$systemData["month_".$str]+$money,
            "day_".$str=>$money,
            "year_time_".$str=>date("Y"),
            "month_time_".$str=>date("m"),
            "day_time_".$str=>strtotime(date("Y-m-d")),
        );
    }else if(time()-$systemData["day_time_".$str]>86400&&$systemData["month_time_".$str]!=date("m")&&$systemData["year_time_".$str]==date("Y"))
    {
        $updateData=array(
            "all_".$str=>$systemData["all_".$str]+$money,
            "year_".$str=>$systemData["year_".$str]+$money,
            "month_".$str=>$money,
            "day_".$str=>$money,
            "year_time_".$str=>date("Y"),
            "month_time_".$str=>date("m"),
            "day_time_".$str=>strtotime(date("Y-m-d")),
        );
    }else if(time()-$systemData["day_time_".$str]>86400&&$systemData["month_time_".$str]!=date("m")&&$systemData["year_time_".$str]!=date("Y"))
    {
        $updateData=array(
            "all_".$str=>$systemData["all_".$str]+$money,
            "year_".$str=>$money,
            "month_".$str=>$money,
            "day_".$str=>$money,
            "year_time_".$str=>date("Y"),
            "month_time_".$str=>date("m"),
            "day_time_".$str=>strtotime(date("Y-m-d")),
        );
    }
    $updateData["id"]=$systemData["id"];
    $res1=Db::name("balance_system")->update($updateData);
    if($res1)
    {
        return true;
    }else
    {
        return false;
    }
}